<?php
/**
 * 《排班模板》实现类
 *
 * @author 孙晓晔
 * @version $Id: Computer.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/KqUtil.class.php');

class TemplatePlan extends KqUtil {
	public $tbl = Constant::tbl_kq_template_plan;

	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'template' => '模板',
		't1' => '上班时间',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'kq/template/plan/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'buydate') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['buydate'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$template_s = $this->_input($request['template_s']);

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$my = $this->_my();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($template_s)) {
			$where .= " AND template = '" . $template_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['template_t'] = Name::template($row['template']);
				$row['week'] = $this->week($row['t1']);
				$row['date'] = $this->_date($row['t1'], $row['t2']);

				$row['oper_t'] = Name::emp($row['oper']);
				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'kq/template/plan/list');
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/template/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$template = $this->_input($request['template']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(template, t1, t2, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $template, $t1, $t2, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function batch($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('template', $this->id(Constant::tbl_kq_template, $id));

		$controller->display($request, 'kq/template/plan/batch');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function batchPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$date1 = $request['date1'];
		$date2 = $request['date2'];

		$time1 = $request['time1'];
		$time2 = $request['time2'];

		$holiday = $request['holiday'];

		$content = addslashes($request['content']);

		debug(print_r($request, true));

		$week = json_decode($request['week']);
		debug(print_r($week, true));
		if (!$week || $week == NULL) {
			debug("week is NULL");
			return false;
		}

		$db = newdb($this->dsn);
		$fmt = "INSERT INTO %s(template, t1, t2, content, oper, createtime, modifytime) ";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";

		$t1 = strtotime($date1);
		$t2 = strtotime($date2);
		for ($i = 0; $t1 < $t2; $i++) {
			$dd = date('Y-m-d', $t1);
			$ww = date('w', $t1);
			$t1 = strtotime("1 day", $t1);

			if (!in_array($ww, $week)) {
				debug($ww);
				continue;
			}

			$from = $dd . " " . $time1;
			$to = $dd . " " . $time2;
			if ($this->_holiday($holiday, $from, $to)) {
				debug("holiday...");
				continue;
			}

			$sql = sprintf($fmt, $this->tbl, $id, $from, $to, $content, $_SESSION['id']);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function plan($controller, $request) {
		$this->pretreat($controller);

		$date1 = "2010-05-01";
		$date2 = "2010-05-31";

		$t1 = strtotime($date1);
		$t2 = strtotime($date2);
		for ($i = 0; $t1 < $t2; $i++) {
			$dd = date('Y-m-d', $t1);
			$ww = date('w', $t1);

			$t1 = strtotime("1 day", $t1);

			$fmt = "%s %s\n";
			$s = sprintf($fmt, $dd, $ww);

			echo $s;
		}

		logdate($controller, $request);
		return true;
	}

	function code() {
		$ret = '';
		$dd = sprintf("%s", date('ymd00'));

		do {
			$dd++;
			$dd = sprintf("%08d", $dd);
			$ret = "A" . $dd;
		} while ($this->code_exist($ret));

		return $ret;
	}

	function code_exist($code) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/template/plan/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET t1 = '%s', t2 = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $t1, $t2, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['week'] = $this->week($row['t1']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/template/plan/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/template/plan/copy');
		return true;
	}

	function t($controller, $request) {
		$s = "[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"]";
		echo $s;
		$a = json_decode($s);

		print_r($a);

		// json_decode;

		logdate($controller, $request);
		return true;
	}
}
?>